// // VulkanTexture.cpp
// #include "VulkanTexture.h"
// #include "VulkanDevice.h"
// #include <stdexcept>
// #include <iostream>

// VulkanTexture::VulkanTexture(VulkanDevice* device, const std::string& texturePath)
//     : device(device), texturePath(texturePath), fromMemory(false) {
// }

// VulkanTexture::VulkanTexture(VulkanDevice* device, const void* pixels, int width, int height, int channels)
//     : device(device), fromMemory(true), externalPixels(pixels), 
//       width(width), height(height), channels(channels) {
//     externalImageSize = width * height * channels;
// }

// VulkanTexture::~VulkanTexture() {
//     cleanup();
// }

// VulkanTexture::VulkanTexture(VulkanTexture&& other) noexcept
//     : device(other.device),
//       texturePath(std::move(other.texturePath)),
//       textureImage(other.textureImage),
//       textureImageMemory(other.textureImageMemory),
//       textureImageView(other.textureImageView),
//       textureSampler(other.textureSampler),
//       width(other.width),
//       height(other.height),
//       format(other.format),
//       channels(other.channels),
//       fromMemory(other.fromMemory),
//       externalPixels(other.externalPixels),
//       externalImageSize(other.externalImageSize) {
    
//     other.textureImage = VK_NULL_HANDLE;
//     other.textureImageMemory = VK_NULL_HANDLE;
//     other.textureImageView = VK_NULL_HANDLE;
//     other.textureSampler = VK_NULL_HANDLE;
// }

// VulkanTexture& VulkanTexture::operator=(VulkanTexture&& other) noexcept {
//     if (this != &other) {
//         cleanup();
        
//         device = other.device;
//         texturePath = std::move(other.texturePath);
//         textureImage = other.textureImage;
//         textureImageMemory = other.textureImageMemory;
//         textureImageView = other.textureImageView;
//         textureSampler = other.textureSampler;
//         width = other.width;
//         height = other.height;
//         format = other.format;
//         channels = other.channels;
//         fromMemory = other.fromMemory;
//         externalPixels = other.externalPixels;
//         externalImageSize = other.externalImageSize;
        
//         other.textureImage = VK_NULL_HANDLE;
//         other.textureImageMemory = VK_NULL_HANDLE;
//         other.textureImageView = VK_NULL_HANDLE;
//         other.textureSampler = VK_NULL_HANDLE;
//     }
//     return *this;
// }

// bool VulkanTexture::create() {
//     if (fromMemory) {
//         return createFromMemory(externalPixels, externalImageSize, width, height);
//     }

//     // 从文件加载纹理
//     stbi_uc* pixels = stbi_load(texturePath.c_str(), &width, &height, &channels, STBI_rgb_alpha);
//     if (!pixels) {
//         std::cerr << "Failed to load texture image: " << texturePath << std::endl;
//         return false;
//     }

//     VkDeviceSize imageSize = width * height * 4; // STBI_rgb_alpha 强制为4通道
//     bool success = createFromMemory(pixels, imageSize, width, height);
    
//     stbi_image_free(pixels);
//     return success;
// }

// bool VulkanTexture::createFromMemory(const void* pixels, VkDeviceSize imageSize, int width, int height) {
//     this->width = width;
//     this->height = height;

//     try {
//         // 创建暂存缓冲区
//         VkBuffer stagingBuffer;
//         VkDeviceMemory stagingBufferMemory;
        
//         device->createBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
//                            VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
//                            stagingBuffer, stagingBufferMemory);

//         // 复制数据到暂存缓冲区
//         void* data;
//         vkMapMemory(device->getDevice(), stagingBufferMemory, 0, imageSize, 0, &data);
//         memcpy(data, pixels, static_cast<size_t>(imageSize));
//         vkUnmapMemory(device->getDevice(), stagingBufferMemory);

//         // 创建纹理图像
//         createImage(width, height, format, VK_IMAGE_TILING_OPTIMAL,
//                    VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
//                    VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
//                    textureImage, textureImageMemory);

//         // 转换布局并复制数据
//         transitionImageLayout(textureImage, format, 
//                             VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
//         copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(width), static_cast<uint32_t>(height));
//         transitionImageLayout(textureImage, format,
//                             VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);

//         // 清理暂存资源
//         vkDestroyBuffer(device->getDevice(), stagingBuffer, nullptr);
//         vkFreeMemory(device->getDevice(), stagingBufferMemory, nullptr);

//         // 创建图像视图和采样器
//         createImageView(textureImage, format, VK_IMAGE_ASPECT_COLOR_BIT);
//         createSampler();

//         return true;

//     } catch (const std::exception& e) {
//         std::cerr << "Failed to create texture: " << e.what() << std::endl;
//         cleanup();
//         return false;
//     }
// }

// void VulkanTexture::cleanup() {
//     VkDevice vkDevice = device->getDevice();
    
//     if (textureSampler != VK_NULL_HANDLE) {
//         vkDestroySampler(vkDevice, textureSampler, nullptr);
//         textureSampler = VK_NULL_HANDLE;
//     }
    
//     if (textureImageView != VK_NULL_HANDLE) {
//         vkDestroyImageView(vkDevice, textureImageView, nullptr);
//         textureImageView = VK_NULL_HANDLE;
//     }
    
//     if (textureImage != VK_NULL_HANDLE) {
//         vkDestroyImage(vkDevice, textureImage, nullptr);
//         textureImage = VK_NULL_HANDLE;
//     }
    
//     if (textureImageMemory != VK_NULL_HANDLE) {
//         vkFreeMemory(vkDevice, textureImageMemory, nullptr);
//         textureImageMemory = VK_NULL_HANDLE;
//     }
// }

// VkDescriptorImageInfo VulkanTexture::getDescriptorInfo() const {
//     VkDescriptorImageInfo imageInfo{};
//     imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
//     imageInfo.imageView = textureImageView;
//     imageInfo.sampler = textureSampler;
//     return imageInfo;
// }

// // 以下实现辅助方法...
// void VulkanTexture::createImage(uint32_t width, uint32_t height, VkFormat format, 
//                               VkImageTiling tiling, VkImageUsageFlags usage, 
//                               VkMemoryPropertyFlags properties, 
//                               VkImage& image, VkDeviceMemory& imageMemory) {
//     VkImageCreateInfo imageInfo{};
//     imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
//     imageInfo.imageType = VK_IMAGE_TYPE_2D;
//     imageInfo.extent.width = width;
//     imageInfo.extent.height = height;
//     imageInfo.extent.depth = 1;
//     imageInfo.mipLevels = 1;
//     imageInfo.arrayLayers = 1;
//     imageInfo.format = format;
//     imageInfo.tiling = tiling;
//     imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
//     imageInfo.usage = usage;
//     imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
//     imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

//     if (vkCreateImage(device->getDevice(), &imageInfo, nullptr, &image) != VK_SUCCESS) {
//         throw std::runtime_error("failed to create image!");
//     }

//     VkMemoryRequirements memRequirements;
//     vkGetImageMemoryRequirements(device->getDevice(), image, &memRequirements);

//     VkMemoryAllocateInfo allocInfo{};
//     allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
//     allocInfo.allocationSize = memRequirements.size;
//     allocInfo.memoryTypeIndex = device->findMemoryType(memRequirements.memoryTypeBits, properties);

//     if (vkAllocateMemory(device->getDevice(), &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) {
//         throw std::runtime_error("failed to allocate image memory!");
//     }

//     vkBindImageMemory(device->getDevice(), image, imageMemory, 0);
// }

// void VulkanTexture::createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
//     VkImageViewCreateInfo viewInfo{};
//     viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
//     viewInfo.image = image;
//     viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
//     viewInfo.format = format;
//     viewInfo.subresourceRange.aspectMask = aspectFlags;
//     viewInfo.subresourceRange.baseMipLevel = 0;
//     viewInfo.subresourceRange.levelCount = 1;
//     viewInfo.subresourceRange.baseArrayLayer = 0;
//     viewInfo.subresourceRange.layerCount = 1;

//     if (vkCreateImageView(device->getDevice(), &viewInfo, nullptr, &textureImageView) != VK_SUCCESS) {
//         throw std::runtime_error("failed to create texture image view!");
//     }
// }

// void VulkanTexture::createSampler() {
//     VkSamplerCreateInfo samplerInfo{};
//     samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
//     samplerInfo.magFilter = VK_FILTER_LINEAR;
//     samplerInfo.minFilter = VK_FILTER_LINEAR;
//     samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
//     samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
//     samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
//     samplerInfo.anisotropyEnable = VK_TRUE;
//     samplerInfo.maxAnisotropy = device->getPhysicalDeviceProperties().limits.maxSamplerAnisotropy;
//     samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
//     samplerInfo.unnormalizedCoordinates = VK_FALSE;
//     samplerInfo.compareEnable = VK_FALSE;
//     samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
//     samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
//     samplerInfo.mipLodBias = 0.0f;
//     samplerInfo.minLod = 0.0f;
//     samplerInfo.maxLod = 0.0f;

//     if (vkCreateSampler(device->getDevice(), &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
//         throw std::runtime_error("failed to create texture sampler!");
//     }
// }

// // 其他辅助方法实现...